package com;

import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

/***
 * 简单的一个锁同步使用样  例
 */
public class JAVAC_T1 {
    public static int count = 0;
    static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch cd = new CountDownLatch(1);
        //创建十个线程：并让其同时起跑
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    cd.await();   //子线程等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int j = 0; j < 1000; j++) {
                 /*   synchronized (JAVAC_T1.class) {
                        count++;
                    }*/
                    lock.lock();
                    count++;
                    lock.unlock();
                }
            }).start();
        }
        Thread.sleep(1000);  //等待子线程创建成功：   也可以计数器设置为10，内层创建一个减一次
        cd.countDown();
        Thread.sleep(2000);
        System.out.println(count);
        System.out.println(count);
        System.out.println(count);
        System.out.println("ihao");
    }
}
